package com.whsxt.service.impl;

import com.whsxt.common.CodeMsg;
import com.whsxt.common.PageInfo;
import com.whsxt.common.Result;
import com.whsxt.dao.CustomerDao;
import com.whsxt.pojo.Customer;
import com.whsxt.service.CustomerService;

import java.util.Map;

/**
 * @Author 武汉尚学堂
 */
public class CustomerServiceImpl implements CustomerService {
    CustomerDao customerDao = new CustomerDao();


    @Override
    public Result queryPage(Map<String, String> params, String page, String limit) {
        PageInfo<Customer> customerPageInfo = customerDao.selectPage(params, page, limit);
        return new Result(customerPageInfo);
    }

    @Override
    public Result addCustomer(String name, String phone, String company, String sex, Integer userId) {
        //业务数据校验
        //客户手机号是唯一的
        //校验手机号的唯一性
        Customer customer = customerDao.selectCustomerByPhone(phone);
        if (customer != null){
            return  new Result(CodeMsg.CUSTOMER_PHONE_EXSIT_ERROR);
        }
        customerDao.insert(name,phone,company,sex,userId);
        return new Result();
    }

    @Override
    public Result updateCustomer(String id, String name, String phone, String company, String sex) {
        //业务数据校验
        //客户手机号是唯一的
        Customer customer = customerDao.selectCustomerByPhone(phone);
        if (customer != null && !customer.getId().toString().equals(id)){
            return  new Result(CodeMsg.CUSTOMER_PHONE_EXSIT_ERROR);
        }
        customerDao.updateById(id,name,phone,company,sex);
        return new Result();
    }


    @Override
    public Result deleteCustomer(String id) {
        customerDao.deleteById(id);
        return new Result();
    }


    @Override
    public Result setCsutomerUserId(String userId, String... customerIds) {
        customerDao.batchUpdateCustomerUserId(userId,customerIds);
        return new Result();
    }
}
